//
//  GYPickerView.m
//  NewProject
//
//  Created by admin on 2017/6/21.
//  Copyright © 2017年 李航飞. All rights reserved.
//

#import "GYPickerView.h"
@interface GYPickerView ()
@property (nonatomic, strong) UIView *bgView;                            //背景
@property (nonatomic, strong) NSString *selectedStr;                     //选中string
@property (nonatomic, strong) UIButton *ensureBtn;                    //确定
@property (nonatomic, strong) UIButton *cancelBtn;                    //取消
@property (nonatomic, strong) UIDatePicker *dataPicker;

@property (nonatomic, strong) NSDate *currentDate;
@end

@implementation GYPickerView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor clearColor];
        
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, HIGHT, WIDTH, 200/WIDTH_6_SCALE)];
        _bgView.backgroundColor = RGB0X(0xe5e5e5);
        
        _dataPicker = [[UIDatePicker alloc]init];
        _dataPicker.frame = CGRectMake(0, 30/WIDTH_6_SCALE, WIDTH, 170/WIDTH_6_SCALE);
        _dataPicker.datePickerMode = UIDatePickerModeDate;
        _dataPicker.backgroundColor = [UIColor whiteColor];
        _dataPicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
        [_dataPicker setMaximumDate:[NSDate date]];
        [_dataPicker addTarget:self action:@selector(birthdayChange:) forControlEvents:UIControlEventValueChanged];
        [_bgView addSubview:_dataPicker];
        
        _ensureBtn = [[UIButton alloc]initWithFrame:CGRectMake(WIDTH-60/WIDTH_6_SCALE, 5/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
        _ensureBtn.layer.cornerRadius = 4;
        _ensureBtn.layer.masksToBounds = YES;
        _ensureBtn.layer.borderColor = RGB0X(0xe4e6ea).CGColor;
        _ensureBtn.layer.borderWidth = 1;
        _ensureBtn.backgroundColor = RGB0X(0xffffff);
        _ensureBtn.titleLabel.font = FONT(13);
        [_ensureBtn setTitle:@"确认" forState:UIControlStateNormal];
        [_ensureBtn setTitleColor:RGB0X(0x696969) forState:UIControlStateNormal];
        [_ensureBtn addTarget:self action:@selector(ensureBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:_ensureBtn];
        
        _cancelBtn = [[UIButton alloc]initWithFrame:CGRectMake(10/WIDTH_6_SCALE, 5/WIDTH_6_SCALE, 50/WIDTH_6_SCALE, 20/WIDTH_6_SCALE)];
        _cancelBtn.layer.cornerRadius = 4;
        _cancelBtn.layer.masksToBounds = YES;
        _cancelBtn.layer.borderColor = RGB0X(0xe4e6ea).CGColor;
        _cancelBtn.layer.borderWidth = 1;
        _cancelBtn.titleLabel.font = FONT(13);
        _cancelBtn.backgroundColor = RGB0X(0xffffff);
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:RGB0X(0x696969) forState:UIControlStateNormal];
        [_cancelBtn addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_bgView addSubview:_cancelBtn];
        
        _currentDate = [NSDate date];//获取当前时间，日期
        //        NSTimeZone *zone  = [NSTimeZone systemTimeZone];
        //        NSInteger interval = [zone secondsFromGMTForDate: date];
        //        _currentDate = [date  dateByAddingTimeInterval: interval];
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        self.selectedStr = [dateFormatter stringFromDate:_currentDate];
    }
    return self;
}

/**
 *  显示pickerview
 */
- (void)showPickerView
{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [[UIApplication sharedApplication].delegate.window addSubview:_bgView];
    [UIView animateWithDuration:0.3 animations:^{
        _bgView.frame = CGRectMake(0, HIGHT - 200/WIDTH_6_SCALE, WIDTH, 200/WIDTH_6_SCALE);
    } completion:^(BOOL finished) {
    }];
}
/**
 *  隐藏pickerview
 */
- (void)hiddenPickerView
{
    [UIView animateWithDuration:0.3 animations:^{
        _bgView.frame = CGRectMake(0, HIGHT, WIDTH, 200/WIDTH_6_SCALE);
    } completion:^(BOOL finished) {
    }];
    [self removeFromSuperview];
}

#pragma mark - action

- (void)ensureBtnClick:(UIButton *)sender
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSDate *endDate = [dateFormatter dateFromString:self.selectedStr];
    //调整时区
    NSInteger endInterval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate: endDate];
    NSDate *end = [endDate dateByAddingTimeInterval: endInterval];
    
    NSUInteger voteCountTime = ([_currentDate timeIntervalSinceDate:end]) / 3600 / 24/365;
    
    NSString *timeStr = [NSString stringWithFormat:@"%lu", (unsigned long)voteCountTime];
    
    NSLog(@"确认被点击");
    if ([_delegate respondsToSelector:@selector(selectedStr:andAge:)])
    {
        [_delegate selectedStr:self.selectedStr andAge:timeStr];
    }
    [self hiddenPickerView];
}

- (void)cancelBtnClick:(UIButton *)sender
{
    NSLog(@"取消被点击");
    [self hiddenPickerView];
}

- (void)birthdayChange:(UIDatePicker *)sender{
    NSDate *myDate = sender.date;
    
    //    NSInteger endInterval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate: myDate];
    //    NSDate *endDate = [myDate dateByAddingTimeInterval: endInterval];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];
    self.selectedStr = [dateFormat stringFromDate:myDate];
}


@end
